package parser.symbol;

import java.util.ArrayList;

/**
 * 数组本身是一个Symbol，有寄存器地址address
 * 数组内部的各个项目，分别都是Symbol
 */
public class MyArray extends Symbol {
    protected int dim; // 数组维数
    protected ArrayList<Integer> size; // 数组每个维度的大小
    protected ArrayList<Symbol> content; // 数组每个项目都是一个变量


    public MyArray(String address) {
        super(address);
        this.content = new ArrayList<>();
    }

    public MyArray(String address, MyArray array) {
        this.dim = array.dim;
        this.size = array.size;
        this.content = array.getContent();
        this.address = address;
    }

    public MyArray(String arrayName, String address, ArrayList<Integer> size) {
        super(address);
        this.name = arrayName;
        this.size = size;
        this.dim = size.size();
        this.content = new ArrayList<>();
    }

    public void setDimAndSize(int dim, ArrayList<Integer> size) {
        this.dim = dim;
        this.size = size;
        setZeroContent();
    }

    public void setZeroContent() {//默认数组元素为0
        int length = getLength();
        if (length < 1000)
            for (int i = 0; i < length; i++) {
                content.add(new MyNumber(0));
            }
    }

    public int getLength() {
        int res = 1;
        for (int i = 0; i < size.size(); i++) {
            res *= size.get(i);
        }
        return res;
    }

    public ArrayList<Symbol> getContent() {
        return content;
    }

    public ArrayList<Integer> getSize() {
        return size;
    }

    public int getDim() {
        return dim;
    }

    @Override
    public String toString() {
        return "MyArray{" +
                "dim=" + dim +
                ", size=" + size +
                ", isConst=" + isConst +
                ", address='" + address + '\'' +
                ", name='" + name + '\'' +
                ", blockId=" + blockId +
                "}\n";
    }
}
